#pragma once

#include "../EntityManager.h"
#include "../Market/Comodities.h"

#include "../Utility.h"

class FinanceManager
{
private:
	struct InstanceData
	{
		uint32 size;
		uint32 capacity;
		void* buffer;

		Entity* entity;				// The entity owning this instance.
		int32* funds;
	};

private:
	// It may be necessary to map entities to indexes.
	InstanceData _data;

private:
	void Allocate(const uint32 capacity);
	void SortData(const uint32 start, const uint32 stop);

public:
	FinanceManager();
	~FinanceManager();

	void RunProduction();

	void PlaceMarketSells(BidData* data) const;
	void PlaceMarketBuys(BidData* data) const;

	void RegisterBuyResults(const BidData* const bid_data, const uint32 num_resources);
	void RegisterSellResults(const BidData* const bid_data, const uint32 num_resources);
	void FindBankruptEntities(OpenVector<Entity>& defunct_entities);
	void AllocateFinance(const Entity* const entities, const uint32 num_entities);
};